<?php

namespace common\models\work;

use Yii;

/**
 * This is the model class for table "{{%substation}}".
 *
 * @property int $id
 * @property int $company_id 直属公司ID
 * @property string $name 变电站名称
 * @property string $voltage 电压等级
 * @property int $voltage_id 电压等级ID
 * @property int $city_company_id 市属公司ID
 * @property int $province_company_id 省属公司ID
 * @property int $listorder 展示排序
 * @property int $status 状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 删除时间
 */
class Substation extends \common\db\ActiveRecord {

    /**
     * 状态
     */
    const STATUS_COMPLETED = 100;
    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%substation}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['company_id', 'voltage_id', 'city_company_id', 'province_company_id', 'listorder', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['name', 'voltage'], 'required'],
            [['name', 'voltage'], 'string', 'max' => 100],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'company_id' => '直属公司ID',
            'name' => '变电站名称',
            'voltage' => '电压等级',
            'voltage_id' => '电压等级ID',
            'city_company_id' => '市属公司ID',
            'province_company_id' => '省属公司ID',
            'uuid' => '统一编码',
            'listorder' => '展示排序',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'deleted_at' => '删除时间',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function getUsers() {
        return $this->hasMany(\common\models\user\User::class, ['company_id' => 'company_id']);
    }

    /**
     * {@inheritdoc}
     */
    public function getCompany() {
        return $this->hasOne(Company::class, ['id' => 'company_id']);
    }

    /**
     * {@inheritdoc}
     */
    public function getCompanyTree() {
        $company = $this->company;
        if (!$company) {
            return [];
        }
        if ($company->parent) {
            return [
                $company->parent->name,
                $company->name,
            ];
        }
        return [$company->name];
    }

}
